package ru.nsu.fit.ask_joe;

import ru.nsu.fit.ask_joe.log_manager.LogManager;
import ru.nsu.fit.ask_joe.log_manager.realization.AndroidLogger;
import ru.nsu.fit.ask_joe.user_interface_manager.interfaces.IUserInterfaceManager;
import ru.nsu.fit.client.core.ClientKernel;
import ru.nsu.fit.client.core.IClientKernel;
import ru.nsu.fit.client.core.KernelConfig;
import ru.nsu.fit.client.core.log_manager.interfaces.ILogManager;
import ru.nsu.fit.client.core.log_manager.interfaces.ILogger;
import ru.nsu.fit.client.core.log_manager.realization.KernelLogManager;

public class MainManager {
    private static ILogManager logManager;
    private static IUserInterfaceManager userInterfaceManager;
    private static IClientKernel clientKernel = null;
    private static ILogger androidLogger = new AndroidLogger("AskJoe:Client");

    private final static String localhost = "http://10.0.2.2:8080/ws/";

    public static ILogManager getLogManager() {
        if (logManager == null) {
            logManager = new LogManager();
            logManager.setLogger(androidLogger);
        }

        return logManager;
    }

    public static void setUserInterfaceManager(IUserInterfaceManager newUserInterfaceManager) {
        userInterfaceManager = newUserInterfaceManager;
    }

    public static IUserInterfaceManager getUserInterfaceManager() {
        return userInterfaceManager;
    }

    public static IClientKernel getClientKernel() {
        if (clientKernel == null) {
            KernelLogManager.staticSetLogger(new AndroidLogger("AskJoe: Kernel"));
            KernelConfig.getDefault().setServerUrl(localhost);
            clientKernel = new ClientKernel();
        }
        return clientKernel;
    }

    public static void changeServerIp(String str) {
        KernelConfig.getDefault().setServerUrl(str);
        System.gc();
        clientKernel = new ClientKernel();
    }
}
